/*
 ============================================================================
 文件名 	: CStockMonitor.cpp
 描述		: DataReader类的实现.
 ============================================================================
 */

//引用
#include <utf.h>
#include <COEMAIN.H>

#include "StockMonitor.h"
_LIT( KFileName, "c:\\stockpricedata.txt");

extern TBool taskCompleted;

// ============================ 成员函数 ===============================

// -----------------------------------------------------------------------------
// CStockMonitor::~CStockMonitor()
// 析构函数.
// -----------------------------------------------------------------------------
//
CStockMonitor::~CStockMonitor()
	{
	Cancel();
	}

// -----------------------------------------------------------------------------
// CStockMonitor::NewLC()
// 两阶段构造函数.
// -----------------------------------------------------------------------------
//
CStockMonitor* CStockMonitor::NewLC()
	{
	CStockMonitor* self = new (ELeave) CStockMonitor();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

// -----------------------------------------------------------------------------
// CStockMonitor::NewL()
// 两阶段构造函数.
// -----------------------------------------------------------------------------
//
CStockMonitor* CStockMonitor::NewL()
	{
	CStockMonitor* self = CStockMonitor::NewLC();
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CStockMonitor::Continue()
// -----------------------------------------------------------------------------
//
void CStockMonitor::Continue()
	{
	//读取用户输入
	iInputKeyCode = iConsole->Getch();
	
	//继续执行
	TRequestStatus* status = &iStatus;
	User::RequestComplete( status, KErrNone );
	SetActive();
	}

// -----------------------------------------------------------------------------
// CStockMonitor::CStockMonitor()
// 析构函数.
// -----------------------------------------------------------------------------
//
CStockMonitor::CStockMonitor() : CActive (EPriorityStandard)
	{
	CActiveScheduler::Add(this);
	}

// -----------------------------------------------------------------------------
// CStockMonitor::ConstructL()
// 两阶段构造函数.
// -----------------------------------------------------------------------------
//
void CStockMonitor::ConstructL()
	{
	iState = ENormal;
	iOriginalPrice = 50;
	iCurrentPrice = iOriginalPrice;
	
	TRAPD(createError, iConsole = Console::NewL(KConsoleTitle, TSize(
				KConsFullScreen, KConsFullScreen)));
	if (createError)
		{
		User::LeaveIfError(createError);
		}
		
	}

// -----------------------------------------------------------------------------
// CStockMonitor::RunL()
// 异步操作完成后由活动对象规划器调用
// -----------------------------------------------------------------------------
//
void CStockMonitor::RunL()
	{
	if( iStatus != KErrNone )
		{
		User::Leave(KErrGeneral);
		}
	
	//过滤掉其他按键事件
	if (!((iInputKeyCode == EKeySpace)
			|| (iInputKeyCode == EKeyUpArrow)
			|| (iInputKeyCode == EKeyDownArrow)))
		{
		Continue();
		return;
		}
	
	//按下"End"键时，结束状态机的执行
	if (iInputKeyCode == EKeySpace)
		{
		CActiveScheduler::Stop();
		return;
		}
	
	switch (iState)
		{
		
		//正常状态下进行降价或涨价
		case ENormal:
			{

			if (iInputKeyCode == EKeyUpArrow)
				{
				if (iCurrentPrice < (iOriginalPrice + KMaxIncrease))
					{
					iCurrentPrice += 1;
					}
				else
					{
					iState = EUpLimit;
					}
				
				}
			else if (iInputKeyCode == EKeyDownArrow)
				{
				if (iCurrentPrice > (iOriginalPrice - KMaxDecrease))
					{
					iCurrentPrice -= 1;
					}
				else 
					{
					iState = EDownLimit;
					}
				}
			}
			break;
		
		//涨停状态下只允许价格下降
		case EUpLimit:
			{
			if (iInputKeyCode == EKeyDownArrow)
				{
				iCurrentPrice -=1;
				iState = ENormal;
				}
			}
			break;
		
		//跌停状态下只允许价格下降
		case EDownLimit:
			{
			if (iInputKeyCode == EKeyUpArrow)
				{
				iCurrentPrice +=1;
				iState = ENormal;
				}			
			}
			break;
		default:
			break;
		}
	
	_LIT(KTextPriceDisplay, " price is = %d\n");
	iConsole->Printf(KTextPriceDisplay, iCurrentPrice);
	
	//继续执行
	Continue();
	}

// -----------------------------------------------------------------------------
// CStockMonitor::DoCancel()
// 析构时由CActive::Cancel()方法调用
// -----------------------------------------------------------------------------
//
void CStockMonitor::DoCancel()
	{
	//空实现
	}

// -----------------------------------------------------------------------------
// CStockMonitor::DoCancel()
// RunL()中发生Leave时，由活动对象规划器调用此函数
// -----------------------------------------------------------------------------
//
TInt CStockMonitor::RunError()
	{
	User::Leave(KErrGeneral);
	}
//文件结束

/*
    stockMonitor = CStockMonitor::NewL();
    CleanupStack::PushL(stockMonitor);
    stockMonitor->Continue();
    CleanupStack::Pop();
 * */
